﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinChatListen.Model;
using WinChatListen.Utils;

namespace WinChatListen
{
    public partial class WinMain : Form
    {
        private NettyServer nettyClient;

        public WinMain()
        {
            InitializeComponent();
        }

        private void WinMain_Load(object sender, EventArgs e)
        {
            nettyClient = new NettyServer();
            nettyClient.ReceiveMessage += NettyClient_ReceiveMessage;
            nettyClient.InitBootstrap();
            var dd = "";
        }

        private void NettyClient_ReceiveMessage(object message)
        {
            //throw new NotImplementedException();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
           bool isLogin = Api.IsLogin();
            if (isLogin)
                labMessage.Text = "已登录";
            else
                labMessage.Text = "未登录";
        }

        private void btnUser_Click(object sender, EventArgs e)
        {
            UserInfo userInfo = Api.getUserInfo();
            labMessage.Text = userInfo.ToString();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            bool isOK = Api.hookMessage();
            if (isOK)
            {
                labMessage.Text = "Hook消息成功";
            }
            else
                labMessage.Text = "Hook消息失败";
        }

        private void btnUnConnect_Click(object sender, EventArgs e)
        {
            bool isOK = Api.unhookMessage();
            if (isOK) { 
                labMessage.Text = "取消Hook消息成功";
            }
            else
                labMessage.Text = "取消Hook消息失败";
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            bool isOK = Api.sendMessageText(txtWxId.Text.Trim(), txtMessage.Text.Trim());
            if (isOK)
                labMessage.Text = "消息发送成功";
            else
                labMessage.Text = "消息发送失败";
        }

        private void btnRoom_Click(object sender, EventArgs e)
        {
            List<Friend> friends = new List<Friend>();
            List<Friend> rooms = new List<Friend>();
            Api.getFriendsRooms(ref friends, ref rooms);

            foreach (var friend in friends)
            {
                lbFriends.Items.Add("微信id：" + friend.wxid + ",名称：" + friend.userName);
            }
            foreach (var room in rooms)
            {
                lbRooms.Items.Add("微信id：" + room.wxid + ",名称：" + room.userName);
            }
        }

        private void btnCopyFriend_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(lbFriends.Items[lbFriends.SelectedIndex].ToString());
        }

        private void btnCopyRoom_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(lbRooms.Items[lbRooms.SelectedIndex].ToString());
        }
    }
}
